package view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import entities.Cesto;
import entities.Controle;
import entities.Crianca;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class WindowMain extends javax.swing.JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6986897661867186848L;
	private JPanel jPanelCampo;
	private JPanel jPanelKids;
	private JPanel jPanelCesto;
	private JPanel jPanelBotoes;
	private JPanel jPanelConsole;
	private static JPanel jPanelLista;
	private ArrayList<JPanelCrianca> Criancas = new ArrayList<JPanelCrianca>();
	private ArrayList<JLabel> IdsCriancas = new ArrayList<JLabel>();
	public static ArrayList<JLabel> Cestos = new ArrayList<JLabel>();
	public static JLabel labelCesto;
	private static JLabel lbCrianca10;
	private static JLabel lbCrianca9;
	private static JLabel lbCrianca8;
	private static JLabel lbCrianca7;
	private static JLabel lbCrianca6;
	private static JLabel lbCrianca5;
	private static JLabel lbCrianca4;
	private static JLabel lbCrianca3;
	private static JLabel lbCrianca2;
	private static JLabel lbCrianca1;
	private ArrayList<JLabel> jLabelInicio = new ArrayList<JLabel>();
	private JTextField jTextFieldTempoEsp;
	private JLabel jLabelTempoEsp;
	private JTextField JTextFieldID;
	private JLabel JLabelID;
	private JButton jButtonAdd;
	private JTextField jTextFieldTempoBrinc;
	private JLabel jLabelTempoBrinc;
	private JButton jButtonIniciar;
	private JLabel jLabelTituloC;
	private JSeparator jSeparator1;
	private JButton jButtonCesto;
	private JTextField jTextFieldCapacidade;
	private JLabel jLabelCapacidade;
	private JLabel jLabelCesto;
	private JLabel jLabelTemBola;
	private JCheckBox jCheckBoxTemBola;
	private JPanelCrianca lastClicked = null;
	private int controle = 0;
	private int countCriancas = 0;
	private int countBolas = 0;
	private JScrollPane jScrollPane1;
	public static JTextArea log;
	public static JLabel[] lbCriancas;
	List<Crianca> criancas = new ArrayList<Crianca>();
	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		
		SwingUtilities.invokeLater(new Runnable() {
			
			public void run() {
				WindowMain inst = new WindowMain();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public WindowMain() {
		super();
		JLabel aux;
		for(int i=0;i<10;i++){
			aux = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("cesto"+i+".png")));
			aux.setVisible(false);
			Cestos.add(aux);
		}
		initGUI();
	}

	private void initGUI() {
		try {
			this.setTitle("Trabalho de SO");
			GroupLayout thisLayout = new GroupLayout(
					(JComponent) getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			{
				jPanelCampo = new JPanel();
				GroupLayout jPanelCampoLayout = new GroupLayout((JComponent)jPanelCampo);
				jPanelCampo.setLayout(jPanelCampoLayout);
				jPanelCampo.setBackground(new Color(00341770076));
				jPanelCampo.setBorder(BorderFactory
						.createLineBorder(Color.black));
				{
					jPanelCesto = new JPanel();
					FlowLayout jPanelCestoLayout = new FlowLayout();
					jPanelCesto.setSize(60, 70);
					jPanelCesto.setLayout(jPanelCestoLayout);
			

					// adiciona a imagem em um label
					
				
					jPanelCesto.add(Cestos.get(0));
					Cestos.get(0).setVisible(true);
					jPanelCesto.add(Cestos.get(1));
					jPanelCesto.add(Cestos.get(2));
					jPanelCesto.add(Cestos.get(3));
					jPanelCesto.add(Cestos.get(4));
					jPanelCesto.add(Cestos.get(5));
					jPanelCesto.add(Cestos.get(6));
					jPanelCesto.add(Cestos.get(7));
					jPanelCesto.add(Cestos.get(8));
					jPanelCesto.add(Cestos.get(9));
					
					//Cesto.setIcon(new ImageIcon(getClass().getClassLoader().getResource("cesto0.png")));
	
					{
						labelCesto = new JLabel();
						jPanelCesto.add(labelCesto);
						labelCesto.setText("Bolas: 0");
					}

				}
				{
					jPanelKids = new JPanel();
					GridBagLayout jPanelKidsLayout = new GridBagLayout();
					jPanelKidsLayout.rowWeights = new double[] { 0.0, 0.1 };
					jPanelKidsLayout.rowHeights = new int[] { 17, 7 };
					jPanelKidsLayout.columnWeights = new double[] { 0.1, 0.1,
							0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 };
					jPanelKidsLayout.columnWidths = new int[] { 7, 7, 7, 7, 7,
							7, 7, 7, 7, 7 };
					jPanelKids.setLayout(jPanelKidsLayout);

				}

				jPanelCampoLayout.setHorizontalGroup(jPanelCampoLayout.createParallelGroup()
					.addComponent(jPanelKids, GroupLayout.Alignment.LEADING, 0, 889, Short.MAX_VALUE)
					.addGroup(GroupLayout.Alignment.LEADING, jPanelCampoLayout.createSequentialGroup()
					    .addGap(290)
					    .addComponent(jPanelCesto, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
					    .addContainerGap(446, Short.MAX_VALUE)));
				jPanelCampoLayout.setVerticalGroup(jPanelCampoLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jPanelKids, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
					.addGap(64)
					.addComponent(jPanelCesto, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE));
			}
			{
				jPanelConsole = new JPanel();
				jPanelConsole.setBackground(Color.white);
				jPanelConsole.setBorder(BorderFactory
						.createLineBorder(Color.black));
				{
					jScrollPane1 = new JScrollPane();
					jPanelConsole.add(jScrollPane1);
					jScrollPane1.setPreferredSize(new java.awt.Dimension(551, 183));
//					jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//					jScrollPane1.setAutoscrolls(true);
//					jScrollPane1.getVerticalScrollBar().setAutoscrolls(true);
					{
						log = new JTextArea();
						jScrollPane1.setViewportView(log);
						log.setEditable(false);
						log.setDragEnabled(true);
						log.setFont(new java.awt.Font("Arial",1,12));
//						log.setPreferredSize(new java.awt.Dimension(506, 178));
					}
				}
			}
			{
				jPanelBotoes = new JPanel();
				GroupLayout jPanelBotoesLayout = new GroupLayout(
						(JComponent) jPanelBotoes);
				jPanelBotoes.setLayout(jPanelBotoesLayout);
				jPanelBotoes.setBackground(Color.RED);
				jPanelBotoes.setBorder(BorderFactory
						.createLineBorder(Color.black));
				{
					JLabelID = new JLabel();
					JLabelID.setText("Id da Criança");
				}
				{
					JTextFieldID = new JTextField();
					JTextFieldID.setText("1");
					JTextFieldID.setEnabled(false);
				}
				{
					jLabelTempoEsp = new JLabel();
					jLabelTempoEsp.setText("Tempo de Espera");
				}
				{
					jLabelTempoBrinc = new JLabel();
					jLabelTempoBrinc.setText("Tempo de Brincadeira");
				}
				{
					jLabelTituloC = new JLabel();
					jLabelTituloC.setText("Criança");
				}
				{
					jSeparator1 = new JSeparator();
					jSeparator1.setOrientation(SwingConstants.VERTICAL);
					jSeparator1.setBackground(Color.black);
				}
				{
					jCheckBoxTemBola = new JCheckBox();
				}
				{
					jLabelTemBola = new JLabel();
					jLabelTemBola.setText("Tem Bola?");
				}
				{
					jLabelCesto = new JLabel();
					jLabelCesto.setText("Cesto");
				}
				{
					jLabelCapacidade = new JLabel();
					jLabelCapacidade.setText("Capacidade");
				}
				{
					jTextFieldCapacidade = new JTextField();
				}
				{
					jButtonCesto = new JButton();
					jButtonCesto.setText("OK");
					jButtonCesto.addActionListener(this);
				}
				{
					jButtonIniciar = new JButton();
					jButtonIniciar.setText("Iniciar");
					jButtonIniciar.setEnabled(false);
					jButtonIniciar.addActionListener(this);
				}
				{
					jTextFieldTempoBrinc = new JTextField();
				}
				{
					jButtonAdd = new JButton();
					jButtonAdd.setText("Adicionar");
					jButtonAdd.addActionListener(this);
					jButtonAdd.setEnabled(false);
				}
				{
					jTextFieldTempoEsp = new JTextField();
				}
				jPanelBotoesLayout.setHorizontalGroup(jPanelBotoesLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanelBotoesLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jLabelTempoEsp, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 7, Short.MAX_VALUE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jButtonAdd, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 55, Short.MAX_VALUE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jLabelTemBola, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 70, Short.MAX_VALUE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(JLabelID, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 24, Short.MAX_VALUE))
					    .addComponent(jLabelTempoBrinc, GroupLayout.Alignment.LEADING, 0, 148, Short.MAX_VALUE)
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addGap(64)
					        .addComponent(jLabelTituloC, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 11, Short.MAX_VALUE)))
					.addGroup(jPanelBotoesLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(JTextFieldID, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jCheckBoxTemBola, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
					        .addGap(60))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jTextFieldTempoEsp, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
					        .addGap(26))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jTextFieldTempoBrinc, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
					        .addGap(26))
					    .addComponent(jButtonIniciar, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE))
					.addGap(21)
					.addComponent(jSeparator1, GroupLayout.PREFERRED_SIZE, 8, GroupLayout.PREFERRED_SIZE)
					.addGroup(jPanelBotoesLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					        .addComponent(jLabelCapacidade, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
					        .addGap(7)
					        .addComponent(jTextFieldCapacidade, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE))
					    .addGroup(jPanelBotoesLayout.createSequentialGroup()
					        .addGap(23)
					        .addGroup(jPanelBotoesLayout.createParallelGroup()
					            .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					                .addComponent(jButtonCesto, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
					                .addGap(26))
					            .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createSequentialGroup()
					                .addPreferredGap(jButtonCesto, jLabelCesto, LayoutStyle.ComponentPlacement.INDENT)
					                .addComponent(jLabelCesto, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)))
					        .addGap(41)))
					.addContainerGap());
				jPanelBotoesLayout.setVerticalGroup(jPanelBotoesLayout.createParallelGroup()
					.addGroup(jPanelBotoesLayout.createSequentialGroup()
					    .addGroup(jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					        .addComponent(jLabelCesto, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					        .addComponent(jLabelTituloC, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					    .addContainerGap(187, 187))
					.addGroup(jPanelBotoesLayout.createSequentialGroup()
					    .addGap(11)
					    .addGroup(jPanelBotoesLayout.createParallelGroup()
					        .addGroup(jPanelBotoesLayout.createSequentialGroup()
					            .addGroup(jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					                .addComponent(jTextFieldTempoBrinc, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addComponent(jLabelTempoBrinc, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE))
					            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					            .addGroup(jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					                .addComponent(JTextFieldID, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addComponent(JLabelID, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					            .addGroup(jPanelBotoesLayout.createParallelGroup()
					                .addGroup(GroupLayout.Alignment.LEADING, jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					                    .addComponent(jTextFieldCapacidade, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                    .addComponent(jLabelCapacidade, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					                .addGroup(jPanelBotoesLayout.createSequentialGroup()
					                    .addGap(13)
					                    .addGroup(jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					                        .addComponent(jCheckBoxTemBola, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
					                        .addComponent(jLabelTemBola, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
					            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					            .addGroup(jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					                .addComponent(jTextFieldTempoEsp, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addComponent(jLabelTempoEsp, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					            .addGap(19)
					            .addGroup(jPanelBotoesLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					                .addComponent(jButtonCesto, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addComponent(jButtonIniciar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addComponent(jButtonAdd, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					            .addGap(32))
					         .addComponent(jSeparator1, GroupLayout.Alignment.LEADING, 0, 178, Short.MAX_VALUE))
					    .addContainerGap(12, 12)));
			}
			{
				jPanelLista = new JPanel();
				//jPanelLista.setBackground(Color.blue);
				jPanelLista.setBorder(BorderFactory
						.createLineBorder(Color.black));
				jPanelLista.setAlignmentX(1.0f);
				jPanelLista.setAlignmentY(1.0f);
				{
					lbCrianca1 = new JLabel();
					jPanelLista.add(getLbCrianca1());
					lbCrianca1.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca1.setText("crianca1");
					lbCrianca1.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca1.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca1.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca1.setVisible(false);
					lbCrianca1.setOpaque(true);
				}
				{
					lbCrianca2 = new JLabel();
					jPanelLista.add(lbCrianca2);
					lbCrianca2.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca2.setText("crianca2");
					lbCrianca2.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca2.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca2.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca2.setVisible(false);
					lbCrianca2.setOpaque(true);
				}
				{
					lbCrianca3 = new JLabel();
					jPanelLista.add(lbCrianca3);
					lbCrianca3.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca3.setText("crianca3");
					lbCrianca3.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca3.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca3.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca3.setVisible(false);
					lbCrianca3.setOpaque(true);
				}
				{
					lbCrianca4 = new JLabel();
					jPanelLista.add(lbCrianca4);
					lbCrianca4.setText("crianca4");
					lbCrianca4.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca4.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca4.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca4.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca4.setVisible(false);
					lbCrianca4.setOpaque(true);
				}
				{
					lbCrianca5 = new JLabel();
					jPanelLista.add(lbCrianca5);
					lbCrianca5.setText("crianca5");
					lbCrianca5.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca5.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca5.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca5.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca5.setVisible(false);
					lbCrianca5.setOpaque(true);
				}
				{
					lbCrianca6 = new JLabel();
					jPanelLista.add(lbCrianca6);
					lbCrianca6.setText("crianca6");
					lbCrianca6.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca6.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca6.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca6.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca6.setVisible(false);
					lbCrianca6.setOpaque(true);
				}
				{
					lbCrianca7 = new JLabel();
					jPanelLista.add(lbCrianca7);
					lbCrianca7.setText("crianca7");
					lbCrianca7.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca7.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca7.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca7.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca7.setVisible(false);
					lbCrianca7.setOpaque(true);
				}
				{
					lbCrianca8 = new JLabel();
					jPanelLista.add(lbCrianca8);
					lbCrianca8.setText("crianca8");
					lbCrianca8.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca8.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca8.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca8.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca8.setVisible(false);
					lbCrianca8.setOpaque(true);
				}
				{
					lbCrianca9 = new JLabel();
					jPanelLista.add(lbCrianca9);
					lbCrianca9.setText("crianca9");
					lbCrianca9.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca9.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca9.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca9.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca9.setVisible(false);
					lbCrianca9.setOpaque(true);
				}
				{
					lbCrianca10 = new JLabel();
					jPanelLista.add(lbCrianca10);
					lbCrianca10.setText("crianca10");
					lbCrianca10.setHorizontalAlignment(SwingConstants.CENTER);
					lbCrianca10.setHorizontalTextPosition(SwingConstants.CENTER);
					lbCrianca10.setFont(new java.awt.Font("Tahoma",1,12));
					lbCrianca10.setPreferredSize(new java.awt.Dimension(100, 16));
					lbCrianca10.setVisible(false);
					lbCrianca10.setOpaque(true);
				}
				
				lbCriancas = new JLabel[]{lbCrianca1,lbCrianca2,lbCrianca3
						,lbCrianca4,lbCrianca5,lbCrianca6,lbCrianca7,lbCrianca8,lbCrianca9,lbCrianca10};
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(jPanelCampo, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 475, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(12)
				        .addComponent(jPanelLista, GroupLayout.PREFERRED_SIZE, 463, GroupLayout.PREFERRED_SIZE)))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(jPanelConsole, GroupLayout.Alignment.LEADING, 0, 188, Short.MAX_VALUE)
				    .addComponent(jPanelBotoes, GroupLayout.Alignment.LEADING, 0, 188, Short.MAX_VALUE))
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(jPanelBotoes, 0, 438, Short.MAX_VALUE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(jPanelConsole, 0, 566, Short.MAX_VALUE))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(jPanelLista, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
				        .addGap(17)
				        .addComponent(jPanelCampo, 0, 872, Short.MAX_VALUE)))
				.addGap(7));
			pack();
			this.setSize(1054, 726);

			JPanelCrianca.windowMainFrame = this;
			for (int i = 0; i < 10; i++) {
				jLabelInicio.add(new JLabel(new ImageIcon(
						"C:\\Users\\teste\\Pictures\\blank.gif")));
				jPanelKids.add(jLabelInicio.get(i), new GridBagConstraints(i,
						1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
						GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}

		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	public ArrayList<JPanelCrianca> getCriancas() {
		return Criancas;
	}

	public void setCriancas(ArrayList<JPanelCrianca> criancas) {
		Criancas = criancas;
	}

	public JButton getjButtonAdd() {
		return jButtonAdd;
	}

	public void setjButtonAdd(JButton jButtonAdd) {
		this.jButtonAdd = jButtonAdd;
		jButtonAdd.setEnabled(false);
	}

	public JButton getjButtonIniciar() {
		return jButtonIniciar;
	}

	public void setjButtonIniciar(JButton jButtonRemove) {
		this.jButtonIniciar = jButtonRemove;
	}

	public JButton getjButtonCesto() {
		return jButtonCesto;
	}

	public void setjButtonCesto(JButton jButtonCesto) {
		this.jButtonCesto = jButtonCesto;
		
	}

	public JLabel getJLabelID() {
		return JLabelID;
	}

	public JPanelCrianca getLastClicked() {
		return lastClicked;
	}

	public void setLastClicked(JPanelCrianca lastClicked) {
		this.lastClicked = lastClicked;
	}

	public ArrayList<JLabel> getCestos() {
		return Cestos;
	}

	public void setCestos(ArrayList<JLabel> cestos) {
		Cestos = cestos;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(jButtonAdd)) {
			boolean temBola = jCheckBoxTemBola.isSelected();
			String id = JTextFieldID.getText();
			JTextFieldID.setText(Integer.toString((Integer.parseInt(id)+1)));
			
			lbCriancas[countCriancas].setText(id);
			lbCriancas[countCriancas].setVisible(true);
			
			lbCriancas[countCriancas].updateUI();
			
			int tempoB = Integer.parseInt(jTextFieldTempoBrinc.getText());
			int tempoE = Integer.parseInt(jTextFieldTempoEsp.getText());
			
			Crianca c = new Crianca(id, tempoB, tempoE, temBola, countCriancas);
			countCriancas++;
			Criancas.add(new JPanelCrianca(c));
			jPanelKids.remove(jLabelInicio.get(controle));
			IdsCriancas.add(new JLabel (Criancas.get(Criancas.size() - 1).getCrianca().getIdCrianca()));
			jPanelKids.add(Criancas.get(Criancas.size() - 1),
					new GridBagConstraints(controle, 1, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.NONE,
							new Insets(0, 0, 0, 0), 0, 0));
			jPanelKids.add(IdsCriancas.get(IdsCriancas.size() - 1),
					new GridBagConstraints(controle, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.NONE,
							new Insets(0, 0, 0, 0), 0, 0));
			jPanelKids.add(new JLabel("Tb: "+c.getTempoBrincando()+"s"),
					new GridBagConstraints(controle, 2, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.NONE,
							new Insets(0, 0, 0, 0), 0, 0));
			jPanelKids.add(new JLabel("Td: "+c.getTempoDescansando()+"s"),
					new GridBagConstraints(controle, 3, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.NONE,
							new Insets(0, 0, 0, 0), 0, 0));
			if(temBola){
				JLabel bola = new JLabel("Tem Bola");
				bola.setForeground(Color.GREEN);
				jPanelKids.add(bola,
					new GridBagConstraints(controle, 4, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.NONE,
							new Insets(0, 0, 0, 0), 0, 0));
				countBolas++;
			}else{
				JLabel bola = new JLabel("Não tem Bola");
				bola.setForeground(Color.RED);
				jPanelKids.add(bola,
						new GridBagConstraints(controle, 4, 1, 1, 0.0, 0.0,
								GridBagConstraints.CENTER, GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
			}
			controle++;
			Criancas.get(Criancas.size() - 1).setBackground(Color.white);
			jPanelKids.updateUI();
			
			if (Criancas.size() == 10) {
				jButtonAdd.setEnabled(false);
			}
			log.setText("A criança "+ c.getIdCrianca() +" foi criada.\n"+log.getText());
			criancas.add(c);
		}
		if (e.getSource().equals(jButtonIniciar)) {
			if(countBolas == 0){
				JOptionPane.showMessageDialog(this, "É necessário pelo menos uma criança com bola.");
			}else{
				if(countBolas < countCriancas - countBolas){
					for(Crianca c : criancas){
						c.setPriority(1);
						c.start();
					}
				}else{
					JOptionPane.showMessageDialog(this, "O número de crianças com bola deve ser menor que o número de crianças sem bola.");
					return;
				}
			}
			
			
		}

		if (e.getSource().equals(jButtonCesto)) {
			if (jTextFieldCapacidade.getText() != null) {
				Cesto.capacidade = Integer.parseInt(jTextFieldCapacidade
						.getText());
				jButtonAdd.setEnabled(true);
				Controle.espacosLivres = new Semaphore(Cesto.capacidade);
				Cestos.get(0).setVisible(true);
				jButtonCesto.setEnabled(false);
				jTextFieldCapacidade.setEnabled(false);
				jButtonAdd.setEnabled(true);
				jButtonIniciar.setEnabled(true);
				labelCesto.setText("Bolas: "+Cesto.bolas);
				
			}
		}

	}
	
	public JLabel getLbCrianca1() {
		return lbCrianca1;
	}
}
